package kr.hkit.powerjava.chapter12.work;

public class Queue implements Structure{
	private String[] arr;
	private int rear;
	private int front;
	private int size;
	
	public Queue() { }	
	public Queue(int size) {
		this.size = size;
		arr = new String[size];
		this.rear = -1;
		this.front = -1;
	}
	public String[] getArr() {
		return arr;
	}
	public void setArr(String[] arr) {
		this.arr = arr;
	}
	public int getRear() {
		return this.rear;
	}
	public void setRear(int rear) {
		this.rear = rear;
	}
	public int getFront() {
		return this.front;
	}
	public void setFront(int front) {
		this.front = front;
	}
	public int getSize() {
		return size;
	}
	public void setSize(int size) {
		this.size = size;
	}

	@Override
	public void push(String s) {
		if(isFull() == true) { //큐가 가득 찼을 경우
			System.out.println("Queue is Full!!");
			System.out.println("rear = "+this.rear+"  front = "+this.front);
			System.out.println();		
		} else { //push
			arr[++this.rear] = s;
			System.out.println("Push "+s+"!");
		}	
	}

	@Override
	public String pop() {
		if(isEmpty() == true) { //큐가 완전히 비었을 경우
			System.out.println("Queue is Empty!!");
			System.out.println("rear = "+this.rear+"  front = "+this.front);
			this.rear = -1;
			this.front = -1; //큐가 비었으므로 rear, front 값 초기화!
			return null;
		} else { //pop
			String s = arr[++this.front];
			arr[this.front] = null;
			System.out.println("Pop "+s+"!");
			return s;
		}
	}

	@Override
	public boolean isEmpty() {
		return this.rear == this.front;
	}

	@Override
	public boolean isFull() {
		return (this.front == -1 && this.rear == size-1);
	}

	@Override
	public void prnStructure() {
		System.out.println();
		System.out.println("**************Queue**************");
		System.out.println("---------------------------------");
		System.out.print("|");
		for (int i = 0; i < arr.length; i++) {
			System.out.print(" "+arr[i]+" 	|");
		}	
		System.out.print("\n---------------------------------\n");
		System.out.println("|rear : "+this.rear+"  "
				+ "front : "+this.front+"		|");
		System.out.print("---------------------------------");
		System.out.println();
	}

}